﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameDataLibrary;

namespace UtopianGame
{
    public class StatusEffect
    {
        public StatusEffectData xmlData;
        public int regionNumber;

        public StatusEffect(StatusEffectData newXMLData)
        {
            xmlData = newXMLData;
            regionNumber = -1;
        }

        public void runBehavior()
        {

            for (int i = 0; i < xmlData.behaviorArray.Length; i++)
            {
                if (xmlData.behaviorArray[i].behaviorFunction == "rashOfRobberies")
                {
                    rashOfRobberies();
                }
                if (xmlData.behaviorArray[i].behaviorFunction == "pettyCrime")
                {
                    pettyCrime();
                }
                if (xmlData.behaviorArray[i].behaviorFunction == "fluSeason")
                {
                    fluSeason();
                }
                if (xmlData.behaviorArray[i].behaviorFunction == "epidemic")
                {
                    epidemic();
                }
                if (xmlData.behaviorArray[i].behaviorFunction == "removeSelf")
                {
                    removeSelf();
                }
                if (xmlData.behaviorArray[i].behaviorFunction == "replaceStatusEffect")
                {
                    replaceStatusEffect(xmlData.behaviorArray[i].behaviorParams[0]);
                }
                if (xmlData.behaviorArray[i].behaviorFunction == "nationwideTax")
                {
                    nationwideTax(Int32.Parse(xmlData.behaviorArray[i].behaviorParams[0]));
                }
                if (xmlData.behaviorArray[i].behaviorFunction == "regionalTax")
                {
                    regionalTax(Int32.Parse(xmlData.behaviorArray[i].behaviorParams[0]));
                }
                if (xmlData.behaviorArray[i].behaviorFunction == "multiplyAverageIncome")
                {
                    multiplyAverageIncome(Double.Parse(xmlData.behaviorArray[i].behaviorParams[0]), Boolean.Parse(xmlData.behaviorArray[i].behaviorParams[1]));
                }
                if (xmlData.behaviorArray[i].behaviorFunction == "changePopulationCount")
                {
                    changePopulationCount(Int32.Parse(xmlData.behaviorArray[i].behaviorParams[0]));
                }
                else if (xmlData.behaviorArray[i].behaviorFunction == "changeMoneyInTreasury")
                {
                    changeMoneyInTreasury(Int32.Parse(xmlData.behaviorArray[i].behaviorParams[0]));
                }
                else if (xmlData.behaviorArray[i].behaviorFunction == "changeCostOfLiving")
                {
                    changeCostOfLiving(Int32.Parse(xmlData.behaviorArray[i].behaviorParams[0]));
                }
                else if (xmlData.behaviorArray[i].behaviorFunction == "changeEducationIndex")
                {
                    changeEducationIndex(Int32.Parse(xmlData.behaviorArray[i].behaviorParams[0]));
                }
                else if (xmlData.behaviorArray[i].behaviorFunction == "changeCrimeIndex")
                {
                    changeCrimeIndex(Int32.Parse(xmlData.behaviorArray[i].behaviorParams[0]));
                }
                else if (xmlData.behaviorArray[i].behaviorFunction == "changePovertyIndex")
                {
                    changePovertyIndex(Int32.Parse(xmlData.behaviorArray[i].behaviorParams[0]));
                }
                else if (xmlData.behaviorArray[i].behaviorFunction == "changeEmploymentIndex")
                {
                    changeAverageIncome(Int32.Parse(xmlData.behaviorArray[i].behaviorParams[0]));
                }
                else if (xmlData.behaviorArray[i].behaviorFunction == "changeResearchIndex")
                {
                    changeResearchIndex(Int32.Parse(xmlData.behaviorArray[i].behaviorParams[0]));
                }
                else if (xmlData.behaviorArray[i].behaviorFunction == "changeIndustrialIndex")
                {
                    changeIndustrialIndex(Int32.Parse(xmlData.behaviorArray[i].behaviorParams[0]));
                }
                else if (xmlData.behaviorArray[i].behaviorFunction == "changeHealthIndex")
                {
                    changeHealthIndex(Int32.Parse(xmlData.behaviorArray[i].behaviorParams[0]));
                }
                else if (xmlData.behaviorArray[i].behaviorFunction == "changeAverageIncome")
                {
                    changeAverageIncome(Int32.Parse(xmlData.behaviorArray[i].behaviorParams[0]));
                }
            }
        }

        private void rashOfRobberies()
        {
            for (int i = 0; i < Nation.regionList.Count; i++)
            {
                if (Nation.regionList[i].crimeIndex > 50)
                {
                    Nation.regionList[i].averageIncome = (int)Math.Round(Nation.regionList[i].averageIncome * 0.9);
                }
            }

            removeSelf();
        }

        private void pettyCrime()
        {
            for (int i = 0; i < Nation.regionList.Count; i++)
            {
                if (Nation.regionList[i].crimeIndex > 25)
                {
                    Nation.regionList[i].crimeIndex = Math.Min(Nation.regionList[i].crimeIndex + 15, 100);
                }
            }

            removeSelf();
        }

        private void fluSeason()
        {
            for (int i = 0; i < Nation.regionList.Count; i++)
            {
                if (Nation.regionList[i].healthIndex < 75)
                {
                    Nation.regionList[i].healthIndex = Math.Max(Nation.regionList[i].healthIndex - 15, 0);
                }
            }

            removeSelf();
        }

        private void epidemic()
        {
            for(int i=0;i<Nation.regionList.Count;i++)
            {
                if(Nation.regionList[i].healthIndex < 50)
                {
                    Nation.regionList[i].costOfLiving += 1000;
                }
            }

            removeSelf();
        }

        private void removeSelf()
        {
            Nation.statusEffectList.Remove(this);
        }

        private void replaceStatusEffect(string statusEffectName)
        {
            StatusEffect statusEffectToAdd = null;
            //find the status effect to add to the Nation.
            for (int i = 0; i < GlobalDataHolder.statusEffectArray.Length; i++)
            {
                if (GlobalDataHolder.statusEffectArray[i].xmlData.name == statusEffectName)
                {
                    statusEffectToAdd = GlobalDataHolder.statusEffectArray[i];
                }
            }
            Nation.statusEffectList.Add(statusEffectToAdd);

            //Add the status effect to the selected region.
            statusEffectToAdd.regionNumber = regionNumber;

            //Remove this status effect from the status effect list.
            Nation.statusEffectList.Remove(this);
        }

        private void nationwideTax(int amount)
        {
            int moneyToAdd = ((Nation.getAverageIncome() * Nation.getPopulationCount()));
            moneyToAdd = ((moneyToAdd * amount) / 100);
            Nation.moneyInTreasury += moneyToAdd;
        }

        private void regionalTax(int amount)
        {
            int moneyToAdd = ((Nation.regionList[regionNumber].averageIncome * Nation.regionList[regionNumber].populationCount));
            moneyToAdd = ((moneyToAdd * amount) / 100);
            Nation.moneyInTreasury += moneyToAdd;
        }

        private void multiplyAverageIncome(double amount, bool isRegionSpecific)
        {
            if (!isRegionSpecific)
            {
                for (int i = 0; i < Nation.regionList.Count; i++)
                {
                    Nation.regionList[i].averageIncome = (int)Math.Round(Nation.regionList[i].averageIncome * amount);
                }
            }
            else
            {
                Nation.regionList[regionNumber].averageIncome = (int)Math.Round(Nation.regionList[regionNumber].averageIncome * amount);
            }
        }

        private void multiplyCostOfLiving(double amount, bool isRegionSpecific)
        {
            if (!isRegionSpecific)
            {
                for (int i = 0; i < Nation.regionList.Count; i++)
                {
                    Nation.regionList[i].costOfLiving = (int)Math.Round(Nation.regionList[i].costOfLiving * amount);
                }
            }
            else
            {
                Nation.regionList[regionNumber].costOfLiving = (int)Math.Round(Nation.regionList[regionNumber].costOfLiving * amount);
            }
        }

        protected void changePopulationCount(int amount)
        {
            int region = regionNumber;
            UtopianGame.Nation.regionList[region].populationCount = Math.Max(0, UtopianGame.Nation.regionList[region].populationCount + amount);
        }

        protected void changeMoneyInTreasury(int amount)
        {
            UtopianGame.Nation.moneyInTreasury += amount;
        }

        protected void changeCostOfLiving(int amount)
        {
            if (regionNumber != -1)
            {
                int region = regionNumber;
                UtopianGame.Nation.regionList[region].costOfLiving = Math.Max(0, UtopianGame.Nation.regionList[region].costOfLiving + amount);
            }
            else
            {
                for (int i = 0; i < Nation.regionList.Count; i++)
                {
                    UtopianGame.Nation.regionList[i].costOfLiving = Math.Max(0, UtopianGame.Nation.regionList[i].costOfLiving + amount);
                }
            }
        }

        protected void changeEducationIndex(int amount)
        {
            if (regionNumber != -1)
            {
                int region = regionNumber;
                UtopianGame.Nation.regionList[region].educationIndex += amount;
                if (Nation.regionList[region].educationIndex > 100)
                {
                    Nation.regionList[region].educationIndex = 100;
                }
                else if (Nation.regionList[region].educationIndex < 0)
                {
                    Nation.regionList[region].educationIndex = 0;
                }
            }
            else
            {
                for (int i = 0; i < Nation.regionList.Count; i++)
                {
                    int region = regionNumber;
                    UtopianGame.Nation.regionList[i].educationIndex += amount;
                    if (Nation.regionList[i].educationIndex > 100)
                    {
                        Nation.regionList[i].educationIndex = 100;
                    }
                    else if (Nation.regionList[i].educationIndex < 0)
                    {
                        Nation.regionList[i].educationIndex = 0;
                    }
                }
            }
        }

        protected void changeCrimeIndex(int amount)
        {
            if (regionNumber != -1)
            {
                int region = regionNumber;
                UtopianGame.Nation.regionList[region].crimeIndex += amount;
                if (Nation.regionList[region].crimeIndex > 100)
                {
                    Nation.regionList[region].crimeIndex = 100;
                }
                else if (Nation.regionList[region].crimeIndex < 0)
                {
                    Nation.regionList[region].crimeIndex = 0;
                }
            }
            else
            {
                for (int i = 0; i < Nation.regionList.Count; i++)
                {
                    int region = regionNumber;
                    UtopianGame.Nation.regionList[i].crimeIndex += amount;
                    if (Nation.regionList[i].crimeIndex > 100)
                    {
                        Nation.regionList[i].crimeIndex = 100;
                    }
                    else if (Nation.regionList[i].crimeIndex < 0)
                    {
                        Nation.regionList[i].crimeIndex = 0;
                    }
                }
            }
        }

        protected void changeHealthIndex(int amount)
        {
            if (regionNumber != -1)
            {
                int region = regionNumber;
                UtopianGame.Nation.regionList[region].healthIndex += amount;
                if (Nation.regionList[region].healthIndex > 100)
                {
                    Nation.regionList[region].healthIndex = 100;
                }
                else if (Nation.regionList[region].healthIndex < 0)
                {
                    Nation.regionList[region].healthIndex = 0;
                }
            }
            else
            {
                for (int i = 0; i < Nation.regionList.Count; i++)
                {
                    int region = regionNumber;
                    UtopianGame.Nation.regionList[i].healthIndex += amount;
                    if (Nation.regionList[i].healthIndex > 100)
                    {
                        Nation.regionList[i].healthIndex = 100;
                    }
                    else if (Nation.regionList[i].healthIndex < 0)
                    {
                        Nation.regionList[i].healthIndex = 0;
                    }
                }
            }
        }
        protected void changePovertyIndex(int amount)
        {
            int region = GlobalDataHolder.currentlySelectedRegion;
            UtopianGame.Nation.regionList[region].povertyIndex = Math.Max(0, UtopianGame.Nation.regionList[region].povertyIndex + amount);
        }
        protected void changeIndustrialIndex(int amount)
        {
            int region = GlobalDataHolder.currentlySelectedRegion;
            UtopianGame.Nation.regionList[region].industrialIndex = Math.Max(0, UtopianGame.Nation.regionList[region].industrialIndex + amount);
        }
        protected void changeResearchIndex(int amount)
        {
            int region = GlobalDataHolder.currentlySelectedRegion;
            UtopianGame.Nation.regionList[region].researchIndex = Math.Max(0, UtopianGame.Nation.regionList[region].researchIndex + amount);
        }
        protected void changeAverageIncome(int amount)
        {
            {
                if (regionNumber != -1)
                {
                    int region = regionNumber;
                    UtopianGame.Nation.regionList[region].averageIncome = Math.Max(0, UtopianGame.Nation.regionList[region].averageIncome + amount);
                }
                else
                {
                    for (int i = 0; i < Nation.regionList.Count; i++)
                    {
                        UtopianGame.Nation.regionList[i].averageIncome = Math.Max(0, UtopianGame.Nation.regionList[i].averageIncome + amount);
                    }
                }
            }
        }

    }
}
